package gameLogic;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class UserSqlHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_NAME = "UsersDatabase.db";

	private static abstract class Users implements BaseColumns {
		private static final String TABLE_NAME = "Users";

		private static final String COLUMN_USERNAME = "Username";
		private static final String COLUMN_PASSWORD = "Password";
		private static final String COLUMN_CURRENT_SCORE = "CurrentScore";
		private static final String COLUMN_CURRENT_EASY_LEVEL = "CurrentEasyLevel";
		private static final String COLUMN_CURRENT_MEDIUM_LEVEL = "CurrentMediumLevel";
		private static final String COLUMN_CURRENT_HARD_LEVEL = "CurrentHardLevel";

	}

	private static final String CREATE_STATEMENT = "CREATE TABLE "
			+ Users.TABLE_NAME + "(" + Users._ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Users.COLUMN_USERNAME
			+ " TEXT, " + Users.COLUMN_PASSWORD + " TEXT, "
			+ Users.COLUMN_CURRENT_SCORE + " INTEGER, "
			+ Users.COLUMN_CURRENT_EASY_LEVEL + " INTEGER, "
			+ Users.COLUMN_CURRENT_MEDIUM_LEVEL + " INTEGER, "
			+ Users.COLUMN_CURRENT_HARD_LEVEL + " INTEGER)";

	private static final String DELETE_STATEMENT = "DROP TABLE "
			+ Users.TABLE_NAME;

	public UserSqlHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void addUser(User user, String password) {

		final SQLiteDatabase db = getWritableDatabase();

		final ContentValues values = createValues(user);
		values.put(Users.COLUMN_PASSWORD, Encrypter.encrypt(password));
		db.insert(Users.TABLE_NAME, null, values);
		db.close();
	}

	private ContentValues createValues(User user) {
		final ContentValues values = new ContentValues();
		values.put(Users.COLUMN_USERNAME, user.getName());
		values.put(Users.COLUMN_CURRENT_SCORE, user.getCurrentScore());
		values.put(Users.COLUMN_CURRENT_EASY_LEVEL, user.getCurrentEasyLevel());
		values.put(Users.COLUMN_CURRENT_MEDIUM_LEVEL,
				user.getCurrentMediumLevel());
		values.put(Users.COLUMN_CURRENT_HARD_LEVEL, user.getCurrentHardLevel());
		return values;
	}

	private User cursorToUser(Cursor cursor) {

		if (!cursor.moveToFirst()) {
			return null;
		}

		User user = new User();

		user.setName(cursor.getString((cursor
				.getColumnIndex(Users.COLUMN_USERNAME))));

		user.setCurrentScore((cursor.getInt((cursor
				.getColumnIndex(Users.COLUMN_CURRENT_SCORE)))));

		user.setCurrentEasyLevel(cursor.getInt((cursor
				.getColumnIndex(Users.COLUMN_CURRENT_EASY_LEVEL))));

		user.setCurrentMediumLevel(cursor.getInt((cursor
				.getColumnIndex(Users.COLUMN_CURRENT_MEDIUM_LEVEL))));

		user.setCurrentHardLevel(cursor.getInt((cursor
				.getColumnIndex(Users.COLUMN_CURRENT_HARD_LEVEL))));

		return user;
	}

	private User extractUser(String username) {

		Cursor cursor = getUserCursor(username);

		return cursorToUser(cursor);
	}

	public User getUser(String username, String password) {

		Cursor cursor = getUserCursor(username);
		password = Encrypter.encrypt(password);

		if (password.equals(cursor.getString(cursor
				.getColumnIndex(Users.COLUMN_PASSWORD)))) {
			return cursorToUser(cursor);
		}

		return null;
	}

	private Cursor getUserCursor(String username) {

		final SQLiteDatabase db = getReadableDatabase();

		final String[] columnsToReturn = { Users.COLUMN_USERNAME,
				Users.COLUMN_CURRENT_SCORE, Users.COLUMN_CURRENT_EASY_LEVEL,
				Users.COLUMN_CURRENT_MEDIUM_LEVEL,
				Users.COLUMN_CURRENT_HARD_LEVEL };

		final String whereClause = Users.COLUMN_USERNAME + " = " + username;

		Cursor cursor = db.query(Users.TABLE_NAME, columnsToReturn,
				whereClause, null, null, null, null);

		db.close();
		return cursor;

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_STATEMENT);
	}

	public void updateUserInfo(User user) {

		final SQLiteDatabase db = getWritableDatabase();

		final ContentValues values = createValues(user);

		final String whereClause = Users.COLUMN_USERNAME + " = "
				+ user.getName();

		db.update(Users.TABLE_NAME, values, whereClause, null);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(DELETE_STATEMENT);
		onCreate(db);

	}

	public boolean usernameExistsInDb(String username) {
		return extractUser(username) != null;
	}
}
